package com.centriole.service.workflow.util;


import java.io.Serializable;
import java.util.List;

/**
 * This class is used to return the results of <code>searchAndCount()</code>
 * operations. It has just two properties: the results and the search and the
 * total (unpaged) count of the search.
 */
public final class SearchResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 当前页，0代表第一页
     */
    private int currentPage = 0;
    /**
     * 页数
     */
    private int pageCount = 0;
    /**
     * 页大小
     */
    private int pageSize = 15;
    /**
     * 总行数
     */
    private long totalCount = -1;
    /**
     * 数据对象
     */
    private List<T> result = null;

    public SearchResult(int currentPage, int pageSize, long totalCount, List<T> result) {
        if (pageSize <= 0) {
            pageSize = 15;
        }
        this.currentPage = currentPage;
        this.pageCount = (int) Math.ceil((double) totalCount / (double) pageSize);
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.result = result;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getPageCount() {
        return pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    /**
     * The total number of results that would have been returned if no
     * maxResults had been specified. (-1 means unspecified.)
     */
    public long getTotalCount() {
        return totalCount;
    }

    /**
     * The results of the search.
     */
    public List<T> getResult() {
        return result;
    }

    /**
     * @return 返回分页数据
     */
    public GridData getPaginationData() {
        return new GridData(result, totalCount);
    }

    public void clear() {
        result.clear();
        result = null;
    }
}
